/**
 * Created by dqw on 2018/04/10.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: false
    },
    {
        id: 'u.userName',
        type: 'string',
        title: '用户名',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'c.name',
        type: 'string',
        title: '品种',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    }, {
        id: 'c.code',
        type: 'string',
        title: '品种代码',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'
    },
    {
        id: 'fee',
        title: '交易综合费',
        type: 'number',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true
    },
    {
        id: 'overnightFee',
        title: '过夜费',
        type: 'number',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        headerClass: 'text-center width-150',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.riskStatus.T1,
            2: $lang.riskStatus.T2,
        }
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        'export':false,
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var editHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            var html = "";
            // 判断是否有编辑权限
            if ($("#riskEdit") && $("#riskEdit").val() == 1) {
                html += editHtml;
            }

            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/user/risk/json/list',
    exportURL: ncGlobal.adminRoot + '/user/risk/json/export',
    exportFileName: '交易品种分控参数表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","delFlag"],Timestamp:["createTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
//排序
grid.sortParameter.columnId = ['asc_id'];
//列表结束

var dtGridColumnsUser = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'type',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'userName',
        title: '用户名   ',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
    },
    {
        id: 'mobile',
        title: '手机号',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
    }
];

var dtGridOptionUser = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/user/json/list',
    exportFileName: '用户列表',
    columns: dtGridColumnsUser,
    gridContainer: 'dtGridContainerUser',
    toolbarContainer: 'dtGridToolBarContainerUser',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id"], Timestamp: ["createTime"]},
    onRowDblClick: function (value, record, column, grid, dataNo, columnNo, cell, row, extraCell, e) {
        if (record.userName != null) {
            $("#chooseUserAdd").text(record.userName);
        }
        else {
            $("#chooseUserAdd").text("");
        }
        $('input[name="userId"]').val(record.id);
        $("#userModal").hide();//隐藏关联栏目窗口
        return false;
    }
};


var grid2 = $.fn.DtGrid.init(dtGridOptionUser);
grid2.fastQueryParameters = new Object();
grid2.fastQueryParameters['eq_delFlag'] = 1;
grid2.fastQueryParameters['eq_type'] = 1;
// 排序
grid2.sortParameter.columnId = ['asc_id'];


//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

          //新增对话框初始化
         $("#addModal").on("show.bs.modal", function (event) {
             //清除错误信息
             $(".alert-danger").remove();
             $("#addForm").psly().reset();
             $("#addForm").find("[name='fee']").val("");
             $("#addForm").find("[name='overnightFee']").val("");
             $(".status").bootstrapSwitch('state', true);

         });

        //编辑对话框初始化
         $("#editModal").on("show.bs.modal", function (event) {
             //清除错误信息
             $(".parsley-type").remove();
             //获取接受事件的元素
             var button = $(event.relatedTarget);
             //获取data 参数
             var datano = button.data('no');
             var modal = $(this);
             //获取列表框中的原始数据
             var gridData = grid.sortOriginalDatas[datano];
             var editForm = $("editForm");
             modal.find('input[name="id"]').val(gridData.id);
             modal.find('input[name="userName"]').val(gridData.u.userName);
             modal.find('input[name="name"]').val(gridData.c.name);
             modal.find('input[name="fee"]').val(gridData.fee);
             modal.find('input[name="overnightFee"]').val(gridData.overnightFee);
             if(gridData.status==1){
                 $(".status").bootstrapSwitch('state', true);
             }else{
                 $(".status").bootstrapSwitch('state', false);
             };
         });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_u.userName'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });

        $("#userModal").on("show.bs.modal", function (event) {
            grid2.fastQueryParameters = new Object();
            grid2.fastQueryParameters['eq_delFlag'] = 1;
            grid2.fastQueryParameters['eq_type'] = 1;
            grid2.pager.startRecord = 0;
            grid2.pager.nowPage = 1;
            grid2.pager.recordCount = -1;
            grid2.pager.pageCount = -1;
            grid2.load();
        });

        //去除输入框回车键提交
        $("input").on("keydown", function (e) {
            if (e.keyCode == 13) {
                e.preventDefault();
                var a = $("#releasePrice");
                a.length && a.trigger("click");
            }
        });

    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        }
    }
}();

$(function () {
    grid.load();
    OperateHandle.init();
})